/**
 * 
 */
package com.maywide.jdp.system.aspect;

import java.util.List;

import javax.annotation.Resource;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.jboss.cache.Cache;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.maywide.jdp.system.bean.BusinessFunctionBean;
import com.maywide.jdp.system.bean.RuleBean;
import com.maywide.jdp.system.entities.BusinessFunction;
import com.maywide.jdp.system.entities.Rule;

/**
 * @author luoyang
 */
@Aspect
@Component("ruleAspect")
public class RuleAspect {
	private Cache<String, Object> cache = null;
	private BusinessFunctionBean businessFunctionBean = null;
	private RuleBean ruleBean = null;
	
	@Resource(name = "cache")
	public void setCache(Cache<String, Object> cache) {
		this.cache = cache;
	}

	@Resource(name="businessFunctionBean")
	public void setBusinessFunctionBean(BusinessFunctionBean businessFunctionBean) {
		this.businessFunctionBean = businessFunctionBean;
	}

	@Resource(name="ruleBean")
	public void setRuleBean(RuleBean ruleBean) {
		this.ruleBean = ruleBean;
	}

	@SuppressWarnings({ "unused" })
	@Order(100)
	@AfterReturning(pointcut="execution(public * com.maywide.jdp.system.service.RuleService.update(..)) and args(ruleVO,..)",returning="retVal",argNames="retVal,ruleVO")
	private void doUpdate(Object retVal,Rule ruleVO) throws Exception {
		Rule ret = (Rule) retVal;
		
		String ruleID = ruleVO.getId();
		List<BusinessFunction> businessFunctions = businessFunctionBean.findByRuleID(ruleID);
		for(BusinessFunction businessFunction : businessFunctions) {
			String path = new StringBuffer("/rules/").append(businessFunction.getModule().getBeanName()).append("/").append(businessFunction.getServerClass()).append(".").append(businessFunction.getServerFunction()).toString();
			cache.put(path, ruleID, ruleVO);
		}
	}

	@SuppressWarnings({ "unused" })
	@Order(100)
	@AfterReturning(pointcut="execution(public * com.maywide.jdp.system.service.RuleService.lock(..)) and args(id,..)",argNames="id")
	private void doLock(String id) throws Exception {
		String ruleID = id;
		Rule rule = (Rule) ruleBean.load(ruleID);
		List<BusinessFunction> businessFunctions = businessFunctionBean.findByRuleID(ruleID);
		for(BusinessFunction businessFunction : businessFunctions) {
			String path = new StringBuffer("/rules/").append(businessFunction.getModule().getBeanName()).append("/").append(businessFunction.getServerClass()).append(".").append(businessFunction.getServerFunction()).toString();
			cache.remove(path, ruleID);
		}
	}

	@SuppressWarnings({ "unused" })
	@Order(100)
	@AfterReturning(pointcut="execution(public * com.maywide.jdp.system.service.RuleService.unlock(..)) and args(id,..)",argNames="id")
	private void doUnLock(String id) throws Exception {
		String ruleID = id;
		Rule rule = (Rule) ruleBean.load(ruleID);
		List<BusinessFunction> businessFunctions = businessFunctionBean.findByRuleID(ruleID);
		for(BusinessFunction businessFunction : businessFunctions) {
			String path = new StringBuffer("/rules/").append(businessFunction.getModule().getBeanName()).append("/").append(businessFunction.getServerClass()).append(".").append(businessFunction.getServerFunction()).toString();
			cache.put(path, ruleID, rule);
		}
	}
}
